** This Do-File replicates Table A6 and Figure A11 from P. Dutronc, A. Tondini "Large Means-Tested Pensions with Informal Labor Markets" ** 

set more off

cd "" /*Input here directory where the PALMS v3.2 (only years from 2002--2015) is stored */ 

use "south_africa_palms.dta", clear

**************************************
** Select Population for Estimation ** 
**************************************

drop if year==2008 | year==2009 /* Exclude years in which the threshold is lowered from 65 to 60 */ 

keep if pop_group==1 | pop_group==2  /* Keep Black and Coloured individuals only, see text for explanation */ 

drop if sex==. | sex==9 /* Exclude observations with missing value for gender */ 

gen married=(marital_status==1) /* 1/0 variable for marital status */ 


keep if age>=55 & age<65 /*Window +/- 5 */ 

** Adjust weights for multiple year estimation ** 

bys year: egen mean_weight=mean(weight) 
replace weight=weight/mean_weight /* Adjust weights to have the same mean across waves */

** Definition of Informal Employment **

gen tot_inf=1 if status==1 & no_soc_sec==1 /*Employees w/o social security contributions */
replace tot_inf=0 if status==1 & no_soc_sec==0 /*Employee sw. social security contributions */
replace tot_inf=1 if status==2 & not_registered==1 /*Self-emplyoed w/o registered business*/
replace tot_inf=0 if status==2 & not_registered==0 /*Self-emplyoed w. registered business*/
replace tot_inf=0 if status==3 | status==4 /*Not Employed*/ 


keep if tot_inf!=. /* Exclude from the estimation those with missing informality status.*/

gen f_emp=(tot_inf==0 & emp==1)


**  Wages and Hours ** 

replace hrs_wrk=0 if status==3 | status==4 /* Set hours equal to zero if not employed */
replace monthly_sal=0 if status==3 | status==4 /* Set monthly salary equal to zero if not employed */

gen hrs_wrk_f=hrs_wrk if f_emp==1 /* hours of work in formal employment */
replace hrs_wrk_f=0 if f_emp==0 /* hours of work in formal employment */

gen hourly_wage=monthly_sal/(hrs_wrk*4.5)

replace monthly_sal=ln(monthly_sal) 
replace hourly_wage=ln(hourly_wage)


drop if hourly_wage==. & emp==1 /* Exclude if salary or hours information is missing */ 


**************************************
************ Regressions ** **********
**************************************

gen OAP_1=(age>=60) /* Indicator variable for being at or above the threshold */ 


gen x=age-60 /* center discontinuity at 0 */ 
gen x2=x^2 

gen post=(year>2008) 

gen yob=year-age  

egen cluster=group(yob pop_group) /* Cluster for standard errors by race*cohort group to account for repeated observations over time in DiDisc estimation, see text for explanation */ 



************
**Table A6 **
************

**********************************************************************************

local f_form x /*Linear Functional Form */ 
*local controls i.Province i.married i.education /* set of control variables */ 

** Males ** 

reg emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA6_male.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg tot_inf  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA6_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg f_emp  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA6_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)
reg hrs_wrk_f  i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==1, cluster(cluster)
outreg2 using tableA6_male.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)

table post [aw=weight] if age==59 & sex==1, c(mean emp mean tot_inf mean f_emp mean hrs_wrk_f)


** Females ** 

reg emp i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA6_female.tex, tex  replace label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg tot_inf i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA6_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post) 
reg f_emp i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA6_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)
reg hrs_wrk_f i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if sex==2, cluster(cluster)
outreg2 using tableA6_female.tex, tex  label alpha( 0.01, 0.05, 0.1) dec(4)  keep(1.OAP_1#1.post)

table post [aw=weight] if age==59 & sex==2, c(mean emp mean tot_inf mean f_emp mean hrs_wrk_f)

**********************************************************************************


erase tableA6_male.tex 
erase tableA6_female.tex
erase tableA6_male.txt 
erase tableA6_female.txt




****************************************************************************************

collapse emp tot_inf f_emp [w=weight], by(age sex year post)  /* Average within-year (with sample weights) */
collapse emp tot_inf f_emp, by(age sex  post)  /* Average across years */


***************
**Figure A11 **
***************

** Panel A -men ** 
****************************************************************************************

preserve 
keep if sex==1

twoway (lfit emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter emp age if post==1, msymbol(square_hollow ) mcolor(black) msize(small)) ///
||, ytitle("Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 



preserve 
keep if sex==1
twoway (lfit f_emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit f_emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter f_emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter f_emp age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Formal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After")) xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 

preserve 
keep if sex==1
twoway (lfit tot_inf age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit tot_inf age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter tot_inf age if post==0, mcolor(black) msize(small)) /// 
|| (scatter tot_inf age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Informal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) /// 
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.3)
restore 

****************************************************************************************


** Panel B - women ** 
****************************************************************************************

preserve 
keep if sex==2

twoway (lfit emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter emp age if post==1, msymbol(square_hollow ) mcolor(black) msize(small)) ///
||, ytitle("Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 


preserve 
keep if sex==2
twoway (lfit f_emp age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit f_emp age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit f_emp age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter f_emp age if post==0, mcolor(black) msize(small)) /// 
|| (scatter f_emp age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Formal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) ///
||, legend(order(5 6) label(5 "Before") label(6 "After")) xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.6)
restore 


preserve 
keep if sex==2
twoway (lfit tot_inf age if age<60 & post==0 ,  range(. 60)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==0,   lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (lfit tot_inf age if age<60 & post==1,  range(. 60) lpattern(dash) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) )  /// 
|| (lfit tot_inf age if age>=60 & post==1,  lpattern(dash)  lcolor(black)  lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none) ) /// 
|| (scatter tot_inf age if post==0, mcolor(black) msize(small)) /// 
|| (scatter tot_inf age if post==1, msymbol(square_hollow) mcolor(black) msize(small)) ///
||, ytitle("Informal Employment") xline(60, lcolor(black) lpattern(dash)) xtitle("Age") graphregion( color(white) ) plotregion(  fcolor(white) ) /// 
||, legend(order(5 6) label(5 "Before") label(6 "After"))  xlabel(55 (1) 64) ylabel(0.05 (0.1) 0.3)
restore 

****************************************************************************************
